package cn.itcast.service.impl;

import cn.itcast.mapper.NoteMapper;
import cn.itcast.entity.Note;
import cn.itcast.service.NoteService;
import cn.itcast.util.AlibabaNoteUtil;
import cn.itcast.util.RandomUtil;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class NoteServiceImpl implements NoteService {
    @Autowired
    private AlibabaNoteUtil alibabaNoteUtil;

    @Autowired
    private NoteMapper noteMapper;
    @Override
   /* @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "simple.queue"),
            exchange = @Exchange(value = "simple.direct"),
            key = "simple"
    ))*/
    public void add(Note note) throws Exception {
        int a = 10000;
        note.setOrderId((long) a++);
        note.setUserId((long) a++);
        // 创建一个HashMap对象，用于存储参数map
        Map<String, Object> map = new HashMap();
        // 调用RandomUtil.getSixBitRandom()方法生成6位随机数，并将其存入map中的"code"键中
        map.put("code", RandomUtil.getSixBitRandom());
        // 调用alibabaNoteUtil.addOrderNote(map, note.getBeSentPhone())方法添加订单笔记，并将返回值存入status变量中
        boolean status = alibabaNoteUtil.addOrderNote(map, note.getBeSentPhone());
        // 设置笔记的签名，值为AlibabaNoteUtil.SIGN_NAME_VALUE
        note.setNoteSignature(AlibabaNoteUtil.SIGN_NAME_VALUE);
        // 设置笔记的模板，值为AlibabaNoteUtil.TPL_CODE_VALUE
        note.setNoteTemplate(AlibabaNoteUtil.TPL_CODE_VALUE);
        // 设置笔记的发送时间，为当前时间
        note.setSendTime(LocalDateTime.now());
        // 根据status的值设置笔记的发送状态，如果status为true则设置为"1"，否则设置为"0"
        note.setSendStatus(status ? "1" : "0");
        // 调用noteMapper的add方法添加信息
        noteMapper.add(note);
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "dl.queue"),
            exchange = @Exchange(value = "dl.direct", type = ExchangeTypes.DIRECT),
            key = "dl"
    ))
    public void dlCounter(String msg) {
        System.out.println("消费者接收到dl.counter的消息：【" + msg + "】");
    }
    //发送订单短信

}
